package task2_1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DetectCoordsTask {

    public static void main(String[] args) throws IOException {
        List<String> lines = readFile();
        if (lines.size() < 3) {
            throw new IllegalArgumentException("less than 3 rows defined");
        }
        double [][] sputniks = new double[lines.size()][];
        for (int i=0;i<lines.size(); i++) {
            sputniks[i] = new double[3];
            String line = lines.get(i);
            String [] numbers = line.split(" ");
            if (numbers.length != 4) {
                throw new IllegalArgumentException("Incorrect row:\n" + line);
            }
            try {
                sputniks[i][0] = Double.parseDouble(numbers[0].trim().replaceAll(",", "."));
                sputniks[i][1] = Double.parseDouble(numbers[1].trim().replaceAll(",", "."));
                sputniks[i][2] = Double.parseDouble(numbers[3].trim().replaceAll(",", "."));
            } catch (Exception e) {
                throw new IllegalArgumentException("Bad number in line:\n" + line);
            }
        }
        Map<String, Integer> coordsMap = new HashMap<String, Integer>();
        for (int i=0; i < sputniks.length - 1; i++) {
            String [] coords = calcСoords(sputniks[i][0], sputniks[i][1], sputniks[i][2], sputniks[i + 1][0], sputniks[i + 1][1], sputniks[i + 1][2]);

            for (String xy : coords) {
                Integer cnt = coordsMap.get(xy);
                cnt = cnt == null ? 1 : (cnt + 1);
                coordsMap.put(xy, cnt);

                if (cnt == 2) {
                    // actually there is no need to search more
                    System.out.println(xy);
                    System.exit(0);
                }
            }
        }
    }

    private static String[] calcСoords(double x1, double y1, double r1, double x2, double y2, double r2) {
        double d = Math.pow(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2), 0.5);
        if (d > r1 + r2) {
            throw new IllegalArgumentException("incorrect sputniks");
        }
        if (d + r1 < r2) {
            throw new IllegalArgumentException("incorrect sputniks");
        }
        if (d + r2 < r1) {
            throw new IllegalArgumentException("incorrect sputniks");
        }
        double a = (r1*r1 - r2*r2 + d * d)/(2*d);
        double h = Math.pow((r1*r1 - a*a), 0.5);
        double tx = h * ((y2 - y1) / d);
        double zx = a * ((x2 - x1) / d);

        double ty = h * ((x2 - x1) / d);
        double zy = a * ((y2 - y1) / d);

        double x01 = x1 + zx - tx;
        double x02 = x1 + zx + tx;

        double y01 = y1 + zy + ty;
        double y02 = y1 + zy - ty;

        return new String[]{
                "" + new BigDecimal(x01).setScale(3, RoundingMode.HALF_DOWN) + " " + new BigDecimal(y01).setScale(3, RoundingMode.HALF_DOWN),
                "" + new BigDecimal(x02).setScale(3, RoundingMode.HALF_DOWN) + " " + new BigDecimal(y02).setScale(3, RoundingMode.HALF_DOWN)
        };
    }

    private static List<String> readFile() throws IOException {
        File f = new File("input.txt");
        if (!f.exists()) {
            throw new IllegalArgumentException("File input.txt not found");
        }

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            String line = null;
            List<String> lines = new ArrayList<String>();
            do {
                line = reader.readLine();
                if (line != null) {
                    lines.add(line);
                }
            } while (line != null);

            return lines;
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

}
